﻿//  --------------------------------
//  Copyright (c) PK Logix LLC. All rights reserved.
//  This source code is made available under the terms of the GNU General Public License version 2 (GPLv2)
//  http://www.codeplex.com/mvcclubsite/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;

namespace MVCClubSite.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "PagedList",                                               // Route name
                "{controller}/List/{page}",                                      // URL with parameters
                new { controller = "Announcements", action = "List", page = "1" }  // Parameter defaults
            );

            routes.MapRoute(
                "SelectAlbumImage",                                                                // Route name
                "Albums/{action}/{id}/{selectedImage}",                                         // URL with parameters
                new { controller = "Albums", action = "Details", id = "", selectedImage = "" } // Parameter defaults
            );

            routes.MapRoute(
                "EventsCalendar",                                                                                                               // Route name
                "Events/Calendar/{month}/{year}",                                                                                               // URL with parameters
                new { controller = "Events", action = "Calendar", month = DateTime.Now.Month.ToString(), year = DateTime.Now.Year.ToString() }  // Parameter defaults
            );



            routes.MapRoute(
                "Images",                                                         // Route name
                "Images/{action}/{id}",                                           // URL with parameters
                new { controller = "Images", action = "OriginalImage", id = "" }  // Parameter defaults
            );
            
            routes.MapRoute(
                "Account",                                                         // Route name
                "Account/Edit/{userName}",                                           // URL with parameters
                new { controller = "Account", action = "Edit", userName = "" }  // Parameter defaults
            );

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
            CreateDefaultRolesIfNoneExist();
            CreateDefaultUserIfNotExist();
        }

        private static void CreateDefaultRolesIfNoneExist()
        {
            if (!Roles.RoleExists("Administrator"))
                Roles.CreateRole("Administrator");
        }

        private static void CreateDefaultUserIfNotExist()
        {
            const string DefaultUser = "admin";
            const string DefaultPassword = "admin";
            const string DefaultEmail = "admin@admin.com";

            if (Membership.GetUser(DefaultUser) != null) return;

            MembershipUser adminUser = Membership.CreateUser(DefaultUser, DefaultPassword, DefaultEmail);

            Roles.AddUserToRole(DefaultUser, "Administrator");
        }
    }
}